<?php
class Template
{
    /**
     * @var object Zend DB Adapter
     */
    private $_db;
    
    public function __construct()
    {
			$this->_db = Zend_Registry::get('db');
    }
    
    public function listTemplates()
    {
    	$this->_db->query("SET NAMES 'utf8'");
    	$getTemplates = $this->_db->fetchAll("
			SELECT template_id, template_title, template_content_en, template_content_ru, module_name
			FROM templates
		");
		
		return $getTemplates;
	}
 	
	public function getTemplate($template_id)
    {
    	$this->_db->query("SET NAMES 'utf8'");
    	$getTemplatedetails = $this->_db->fetchrow("
			SELECT template_id, template_title, template_content_en, template_content_ru, module_name
			FROM templates 
			WHERE template_id=" . $template_id			
		);
		return $getTemplatedetails;       
    }
    
    public function getTemplateInfo() 
    {
    	$this->_db->query("SET NAMES 'utf8'");
    	$getTplArr = $this->_db->fetchAll("
        	SELECT template_id, template_title 
        	FROM templates
        ");
    	
    	return $getTplArr;
    }
    
    public function getTemplatenewsletter($template_id)
    {
    	$this->_db->query("SET NAMES 'utf8'");
    	$geTemplatedetails = $this->_db->fetchrow("
			SELECT template_content_en, template_content_ru
			FROM templates 
			WHERE template_id=".$template_id			
		);
	
		return $geTemplatedetails;       
    }

    public function addTemplate($form_values)
    { 
        //print_r($form_values); exit;
        $module_name = $form_values['module_name'];
        $template_title = $form_values['template_title'];
        $template_content_en = $form_values['template_content_en'];
        $template_content_ru = $form_values['template_content_ru'];
        
        $this->_db->query("SET NAMES 'utf8'");
        $addTemplate = $this->_db->exec("
            INSERT INTO templates (module_name, template_title, template_content_en, template_content_ru) 
            VALUES ('" . $module_name . "', '" . $template_title . "', '" . $template_content_en . "', '" . $template_content_ru . "')
	    ");
    	
        if($addTemplate)
        {
            echo "New Template has been successfully added";
        }
    }

    public function updateTemplate($form_values)
    {
        $template_id = $form_values['template_id'];
    	$module_name = $form_values['module_name'];
        $template_title = $form_values['template_title'];
        $template_content_en = $form_values['template_content_en'];
        $template_content_ru = $form_values['template_content_ru'];
        
        $this->_db->query("SET NAMES 'utf8'");
        $UpdateTemplate = $this->_db->exec("
        	UPDATE templates SET template_title = '" . $template_title . "' ,template_content_en= '" . $template_content_en . "',template_content_ru= '" . $template_content_ru . "', module_name = '" . $module_name . "' 
       		WHERE template_id= " . $template_id);
    	/*$command="call proc_newsletter('EDIT','".$newsletter_title."','".$newsletter_content."',".$template_id.",".$newsletter_id.",1)";
    	//echo  $command;exit;
		$EditTemplates = $this->_db->exec($command);*/
		if($UpdateTemplate) {
			echo "Template details has been successfully updated.";
		}
    }

    public function deleteTemplate($template_id)
    {
    	$command="DELETE FROM templates WHERE template_id= " . $template_id;
       	$deleteTemplates = $this->_db->exec($command);  
       	if($deleteTemplates) {
       		echo "Selected template has been successfully deleted";
       	}
    }

}

